package com.yami.shop.listener;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.event.UpdateCopyrightEvent;
import com.yami.shop.bean.model.CopyrightWorks;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.bean.Minio;
import com.yami.shop.common.bean.Works;
import com.yami.shop.common.util.OssMultipartFile;
import com.yami.shop.common.util.pdfUtil.PdfUtils;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.service.AttachFileService;
import com.yami.shop.service.CopyrightWorksService;
import com.yami.shop.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Component("CopyrightRightsListener")
@AllArgsConstructor
public class CopyrightRightsListener {


    @Autowired
    private CopyrightWorksService copyrightWorksService;
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private AttachFileService attachFileService;
    @Autowired
    private UserService userService;

    /**
     * 功能描述: 持仓转移产证数据修改
     * @Author: LSR
     * @Date: 2023/6/19 11:47
     */
    @EventListener(UpdateCopyrightEvent.class)
    public void updatePersonRights(UpdateCopyrightEvent event) throws Exception {

        // 通过tokenId查找产证数据信息
        QueryWrapper<CopyrightWorks> copyrightWorksQueryWrapper = new QueryWrapper<>();
        copyrightWorksQueryWrapper.eq("user_id",event.getTokenId());
        copyrightWorksQueryWrapper.eq("token_id",event.getUserId());
        CopyrightWorks copyrightWorks = copyrightWorksService.getOne(copyrightWorksQueryWrapper);

        if(ObjectUtil.isEmpty(copyrightWorks)){
            return;
        }
        // 修改图片信息
        Minio minio = shopConfig.getMinio();
        // 获取 模板
        Works worksConfig = shopConfig.getWorksConfig();
        Map<String,Object> keyWordMap = new HashMap<>();
        // 获取用户实名信息
        User user = userService.getById(event.getToUserId());
        keyWordMap.put("name",user.getRealName());
        // 通过PDF模板生成图片
        URL url = new URL(minio.getEndpoint() + "/" + minio.getBucketName() + "/" + worksConfig.getCopyrightWorksTemplate());
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();
        ByteArrayOutputStream qrOutputStream = new ByteArrayOutputStream();
        PdfUtils.generatePdf(urlConnection.getInputStream(), qrOutputStream, keyWordMap,null,35f);
        //保存图片
        PDDocument document = PDDocument.load(qrOutputStream.toByteArray());
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        BufferedImage image = pdfRenderer.renderImageWithDPI(0, 300); // 使用300dpi解析第一页，可以自行调整dpi大小
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", outputStream);
        MultipartFile ossMultipartFile = new OssMultipartFile("111.jpg", ".jpg", outputStream.toByteArray());
        String fileUrl = attachFileService.uploadFile(ossMultipartFile.getBytes(), UUID.randomUUID() + ".jpg");

        // 产证数据修改
        copyrightWorks.setWorksCoverUrl(fileUrl);
        copyrightWorksService.updateById(copyrightWorks);
    }

}
